package table_data;

import beans.Unit;
import common.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TransUintKbseToLocal {
    public static void main(String[] args) {
        try {
            File file=new File("C:\\Users\\ts647\\Desktop\\output_unit");//文件不存在则创建
            FileOutputStream fos=new FileOutputStream(file,true);//第二个参数为true代表追加，为false(默认)为覆盖
            Class.forName(Utils.driver);//加载KBase驱动
            Connection conn = DriverManager.getConnection(Utils.unit_url, Utils.user, Utils. password);// 获取连接
            Statement statement = conn.createStatement();//得到声明
            //当kbase某列值为null（看起来就是空）时，该列也存在，例子："unit_type_code":""
            ResultSet rs = statement.executeQuery("SELECT * FROM UNITSCHANGE");//执行SQL语句返回结果集
            while (rs.next()) { //循环输出结果集
                String unit = rs.getString("单位");
                String aim_unit = rs.getString("目标单位");
                String bi_zhi = rs.getString("换算比值");
                String unit_type = rs.getString("单位类型");
                String unit_type_code = rs.getString("单位类型代码");
                String normal_unit = rs.getString("标准单位");
                String normal_bizhi = rs.getString("标准换算比值");
                Unit u= new Unit(unit,aim_unit,bi_zhi,unit_type,unit_type_code,normal_unit,normal_bizhi);
                fos.write(u.toString().getBytes());
            }
            fos.close();
            rs.close();//关闭结果集
            conn.close();//关闭数据库连接
        } catch (Exception e) {
            System.out.print("KBase ERROR:" + e.getMessage());
        }
    }
}

